package com.example.donghuihui.androidframe.widget;

import android.content.Context;
import android.graphics.drawable.Drawable;
import android.util.AttributeSet;
import android.view.View;
import android.view.ViewGroup;
import android.widget.RadioButton;
import android.widget.RadioGroup;

import com.example.donghuihui.androidframe.R;
import com.example.donghuihui.androidframe.utils.BaseUtil;


/**
 * 筛选单选样式
 */
public class FilterSelectView extends RadioGroup {

//    private static final int PADDING_HOR = 24;//水平方向padding
//    private static final int PADDING_VERTICAL = 32;//垂直方向padding

    private int mSizeTop = 16;
    private int mSizeLeft = 24;

    public FilterSelectView(Context context) {
        super(context);
        int currentScreenWidth = BaseUtil.getScreenWidth(context);

        if (currentScreenWidth >= 1080) {
            mSizeTop = 32;
            mSizeLeft = 40;
        }
    }

    public FilterSelectView(Context context, AttributeSet attrs) {
        super(context, attrs);
        int currentScreenWidth = BaseUtil.getScreenWidth(context);
        if (currentScreenWidth >= 1080) {
            mSizeTop = 32;
            mSizeLeft = 40;
        }
    }

    @Override
    public void addView(View child, int index, ViewGroup.LayoutParams params) {
        if (child instanceof RadioButton) {
            RadioButton radioBtn = (RadioButton) child;
            Drawable drawable = getResources().getDrawable(R.drawable.rbtn_gray);
//            drawable.setBounds(0, 0, drawable.getIntrinsicWidth(), drawable.getIntrinsicHeight());
//            radioBtn.setCompoundDrawables(null, drawable, null, null);
            //去掉左侧圆圈
            radioBtn.setTextColor(getResources().getColorStateList(R.color.color_rbtn));
            radioBtn.setTextSize(16);
            radioBtn.setBackgroundDrawable(drawable);
            radioBtn.setButtonDrawable(android.R.color.transparent);
            radioBtn.setPadding(mSizeLeft, mSizeTop, mSizeLeft, mSizeTop);
        }
        super.addView(child, index, params);
    }

    @Override
    protected void onLayout(boolean changed, int l, int t, int r, int b) {
        int x = 0;// 横坐标开始
        int y = 0;//纵坐标开始
        int specWidth = r - l;
        x = l+mSizeLeft;
        int childCount = getChildCount();
        for (int index = 0; index < childCount; index++) {
            View childView = getChildAt(index);
            childView.measure(MeasureSpec.UNSPECIFIED, MeasureSpec.UNSPECIFIED);
            int width = childView.getMeasuredWidth();
            int height = childView.getMeasuredHeight();
            x += width;
            if (x+mSizeLeft <= specWidth) {
                if (index > 0){
                    x += (mSizeLeft);
                }
            } else {
                x = mSizeLeft;
                x += (width);
                y += (height + mSizeTop);
            }
            childView.layout(x - width, y, x, y + height);

        }
    }

    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        int x = 0;//横坐标
        int y;//纵坐标unravelunravel
        int rows = 1;//总行数
        int specWidth = MeasureSpec.getSize(widthMeasureSpec);
        int childCount = getChildCount();

        int height = 0;
        for (int index = 0; index < childCount; index++) {
            View child = getChildAt(index);
            child.measure(MeasureSpec.UNSPECIFIED, MeasureSpec.UNSPECIFIED);
            int width = child.getMeasuredWidth();
            height = child.getMeasuredHeight();
            x += width;
            if (x + mSizeLeft*2> specWidth) {//换行
                rows++;
                x = width;
            } else {
                x += (mSizeLeft);
            }
        }
        if (rows == 1) {
            y = rows * height;
            specWidth += mSizeLeft;
        } else {
            y = rows * height + (rows - 1) * (mSizeTop);
        }

        setMeasuredDimension(specWidth, y);
    }

    /**
     * 获取选中单选按钮的Text
     *
     * @return
     */
    public String getChildText() {
        int childCount = getChildCount();
        int checkedId = getCheckedRadioButtonId();
        for (int i = 0; i < childCount; i++) {
            View child = getChildAt(i);
            if (child instanceof RadioButton) {
                if (checkedId == child.getId()) {
                    return ((RadioButton) child).getText().toString();
                }
            }
        }
        return null;
    }

}
